/*  This file is part of the KDE project
    Copyright (C) 2004-2007 Matthias Kretz <kretz@kde.org>

    This program is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) version 3.

    This library is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
    Boston, MA 02110-1301, USA.

*/

#include "snapshoteditor.h"

#include <krun.h>
#include <kservicetypeprofile.h>
#include <kservicetypetrader.h>
#include <kconfig.h>
#include <QtCore/QStringList>
#include <QtGui/QListWidget>
#include <kapplication.h>
#include <kicon.h>
#include <QtCore/QList>
#include <QtDBus/QtDBus>
#include <kcmoduleproxy.h>
#include <QDebug>
#include <QInputDialog>


SnapshotEditor::SnapshotEditor(QWidget *parent)
    : QWidget(parent)
{
    setupUi(this);
    m_editortab=new SnapshotEditorTab();
    formLayout->addWidget(m_editortab);
    load();
}

void SnapshotEditor::load()
{
  model = new SnapshotListModel();
  listView->setModel(model);
}

void SnapshotEditor::save()
{
m_editortab->save();
KConfig config("wormholerc");
KConfigGroup snapshotGroup( &config, "Snapshot" );
int i=0;
foreach(Snapshot *s, *model->getList()){
  if(s){
    KConfigGroup snapshotItemGroup( &snapshotGroup, s->name());
    snapshotItemGroup.writeEntry( "Name", s->name());
    snapshotItemGroup.writeEntry( "BaseDir", s->basedir().path());
    snapshotItemGroup.writeEntry( "DestDir", s->destdir().path());

    snapshotItemGroup.config()->sync();
  }
}

snapshotGroup.config()->sync();
}

void SnapshotEditor::defaults()
{
  /*  foreach (KCModuleProxy *proxy, m_kcms) {
        if (proxy) {
            proxy->defaults();
        }
    }

    loadServices(KServiceTypeTrader::self()->defaultOffers("PhononBackend"));*/
}


void SnapshotEditor::addSnapshot()
{
 bool ok;
     QString text = QInputDialog::getText(this, tr("Add snapshot"),
                                          tr("Snapshot name:"), QLineEdit::Normal,
                                          "",&ok);
if(!text.isEmpty() && ok)
{
  Snapshot *s=new Snapshot();
  s->setName(text);
  model->addSnapshot(s);
listView->setCurrentIndex(model->index(model->getList()->indexOf(s), 0));
selectedItemChanged(model->index(model->getList()->indexOf(s), 0));
    //TODO: gestire meglio omonimi
   emit changed();
}
     //snapshotList->addItem(text);
}

void SnapshotEditor::removeSnapshot()
{
/*qDebug() << "remove" << snapshotList->currentRow();*/
model->removeSnapshot(listView->currentIndex());
 emit changed();
}
void SnapshotEditor::selectedItemChanged(QModelIndex i)
{
  qDebug() << "selected " << i.row();
  Snapshot *s=model->getList()->at(i.row());
  edit(s);
}

void SnapshotEditor::edit(Snapshot *s)
{
 // m_namelabel->setText(s->name());
m_editortab->editSnapshot(s);
}

#include "snapshoteditor.moc"

// vim: sw=4 ts=4